package com.zycfc.zsf.boot.redis.util;

import com.zycfc.zsf.boot.redis.domain.*;
import com.zycfc.zsf.boot.redis.*;
import com.zycfc.zsf.boot.redis.enums.*;
import com.zycfc.zsf.boot.util.net.*;
import com.zycfc.zsf.boot.util.json.*;
import java.util.*;
import org.slf4j.*;

public class RedisMonitorUtil
{
    private static final Logger log;
    
    public static void doLog(final RedisMonitor redisMonitor) {
        RedisMonitorUtil.log.info(redisMonitor.toString());
    }
    
    public static <T> RedisMonitor assembleRedisMonitor(final String key, final T value, final RedisOperTypeEnum operTypeEnum, final IRedisOperater redisOperater, final String appName, final boolean notProdEnv) {
        if (notProdEnv && "on".equals(redisOperater.get("zsf:redisMor:checkSwitch", false, CacheTypeEnum.LOCAL, 120L, "off", false))) {
            final Map<String, String> reportedRedisKeyMap = redisOperater.hgetAll(false, "zsf:reportedRedisKey", true);
            boolean isKeyValid = false;
            if (reportedRedisKeyMap != null && reportedRedisKeyMap.size() > 0) {
                for (final String reportedKey : reportedRedisKeyMap.keySet()) {
                    if (key.contains(reportedKey)) {
                        isKeyValid = true;
                        break;
                    }
                }
                if (!isKeyValid) {
                    RedisMonitorUtil.log.error("redis-key:{}\u672a\u62a5\u5907\uff0c\u62d2\u7edd\u8bf7\u6c42", (Object)key);
                    throw new RuntimeException("\u672a\u62a5\u5907\u7684redis-key:" + key);
                }
            }
        }
        if (!"on".equals(redisOperater.get("zsf:redisMor:globalSwitch", true, CacheTypeEnum.LOCAL, 120L, "off", false)) && !"on".equals(redisOperater.get("zsf:redisMor:appSwitch:" + appName, true, CacheTypeEnum.LOCAL, 120L, "off", false)) && !"on".equals(redisOperater.get("zsf:redisMor:nodeSwitch:" + NetUtils.getLocalHost(), true, CacheTypeEnum.LOCAL, 120L, "off", false))) {
            return null;
        }
        final RedisMonitor redisMonitor = new RedisMonitor();
        redisMonitor.setStartTime(System.currentTimeMillis());
        redisMonitor.setMethod(operTypeEnum.name());
        if (value == null) {
            redisMonitor.setValueSize(0);
            redisMonitor.setParseTm(0L);
            return redisMonitor;
        }
        String val = null;
        long parseTm = 0L;
        if (value instanceof String) {
            val = (String)value;
        }
        else {
            final long begin = System.currentTimeMillis();
            try {
                val = JsonUtils.objectToJson((Object)value);
            }
            catch (Throwable e) {
                RedisMonitorUtil.log.error("value\u5e8f\u5217\u5316\u4e3ajson\u4e32\u53d1\u751fException:", e);
            }
            parseTm = System.currentTimeMillis() - begin;
        }
        redisMonitor.setParseTm(parseTm);
        if (val != null) {
            redisMonitor.setValueSize(val.length());
            redisMonitor.setValue((val.length() <= 2000) ? val : val.substring(0, 2000));
        }
        else {
            redisMonitor.setValueSize(0);
        }
        return redisMonitor;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)RedisMonitorUtil.class);
    }
}
